#初始化参数信息
JavaHomePath="/home/www/data/jdk1.8.0_241"
TomcatPath="/home/www/data/apache-tomcat-9.0.34"

echo "JDK路径:"${JavaHomePath}" Tomcat部署路径:"${TomcatPath}

rebootVersion=`date +%Y%m%d-%H%M%S`

#将文件路径切换到Tomcat
cd $TomcatPath

#检查日志备份文件夹是否存在
if [ ! -d logs_bak  ];then
  mkdir logs_bak
else
  echo "日志备份文件夹已经存在"
  # 查找logs_bakx下7天前的文件夹或文件并删除
  find ./logs_bak -mtime +7 -name "*.*" -exec rm -Rf {} \;
  echo "查找logs_bakx下7天前的文件夹或文件并删除完成..."
fi

#将Tomcat线程threaddump存档
export JAVA_HOME=${JavaHomePath}
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
jstack -l `ps -ef | grep ${TomcatPath}|grep java|awk '{print $2}'` > logs_bak/Tomcat-Threaddump存档_${rebootVersion}.txt
echo "Tomcat线程threaddump存档完成..."

#将Tomcat进程Kill掉
kill -9 `ps -ef | grep ${TomcatPath}|grep java|awk '{print $2}'`
echo "Tomcat进程已完成Kill..."


#将日志打包备份至日志备份文件夹
logsfile=logs_bak_${rebootVersion}.tar.gz
echo "将日志打包备份至日志备份文件夹:"${logsfile}

tar -Pzcf $logsfile ${TomcatPath}/logs/*
mv $logsfile ${TomcatPath}/logs_bak/
echo "日志文件备份完成..."

#清空现有日志文件
rm -rf $TomcatPath/logs/*

#静默方式启动Tomcat
cd $TomcatPath/bin
nohup ./startup.sh 1> $TomcatPath/logs/catalina.out 2> $TomcatPath/logs/catalina.err &

echo "Tomcat启动完成,等待输出日志..."

#检查启动日志是否输出,输出后Tail方式打开
sleep 3s
tail -F $TomcatPath/logs/catalina*.out
